package cn.admobiletop.adsuyi.adapter.toutiao.listener;

import android.os.Handler;
import android.os.Looper;
import android.view.View;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;

import cn.admobiletop.adsuyi.ad.ADSuyiInterstitialAd;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiInterstitialAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.data.InterstitialAdInfo;
import cn.admobiletop.adsuyi.config.ADSuyiErrorConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;
import cn.admobiletop.adsuyi.util.ADSuyiPackageUtil;

/**
 * @author ciba
 * @description 插屏广告监听
 * @date 2020/4/3
 */
public class InterstitialAdListener extends BaseAdListener<ADSuyiInterstitialAdListener> implements TTAdNative.NativeExpressAdListener {
    private ADSuyiInterstitialAd interstitialAd;
    private InterstitialAdInfo interstitialAdInfo;
    private Handler handler = new Handler(Looper.getMainLooper());

    public InterstitialAdListener(String platformPosId, ADSuyiInterstitialAd interstitialAd, ADSuyiInterstitialAdListener adListener) {
        super(platformPosId, adListener);
        this.interstitialAd = interstitialAd;
    }

    @Override
    public void onError(final int code, final String message) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onAdFailed(code, message);
                }
            });
        }
    }

    @Override
    public void onNativeExpressAdLoad(List<TTNativeExpressAd> nativeExpressAdList) {
        if (nativeExpressAdList == null || nativeExpressAdList.isEmpty()) {
            if (handler != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
                    }
                });
            }
        } else if (getAdListener() != null && !ADSuyiAdUtil.isReleased(interstitialAd)) {
            TTNativeExpressAd ttNativeExpressAd = nativeExpressAdList.get(0);
            if (ttNativeExpressAd == null) {
                if (handler != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
                        }
                    });
                }
                return;
            }
            interstitialAdInfo = new InterstitialAdInfo(interstitialAd.getActivity(), getPlatformPosId());
            interstitialAdInfo.setAdapterAdInfo(ttNativeExpressAd);
            interstitialAdInfo.setAdListener(getAdListener());

            // 设置广告监听事件
            ttNativeExpressAd.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
                @Override
                public void onAdDismiss() {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null && interstitialAdInfo != null) {
                                    getAdListener().onAdClose(interstitialAdInfo);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onAdClicked(View view, int type) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null && interstitialAdInfo != null) {
                                    getAdListener().onAdClick(interstitialAdInfo);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onAdShow(View view, int i) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null && interstitialAdInfo != null) {
                                    getAdListener().onAdExpose(interstitialAdInfo);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onRenderFail(View view, final String msg, final int code) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                onAdFailed(code, msg);
                            }
                        });
                    }
                }

                @Override
                public void onRenderSuccess(View view, float v, float v1) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null && interstitialAdInfo != null) {
                                    getAdListener().onAdReceive(interstitialAdInfo);
                                    getAdListener().onAdReady(interstitialAdInfo);
                                }
                            }
                        });
                    }
                }
            });

            ttNativeExpressAd.render();
        }
    }

    @Override
    public void release() {
        super.release();
        interstitialAd = null;
        if (interstitialAdInfo != null) {
            interstitialAdInfo.release();
            interstitialAdInfo = null;
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
